package guanqu.server;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.alibaba.fastjson.JSON;

import guanqu.beans.IrrBAWaterGate;
import guanqu.beans.RealTimeIrrBRWaterDam;
import manager.PersistentManager;

/**
 * 配水，根据选择的渠道，获取到渠道所在所有的闸门的最新信息，并与渠道的设计水位列在一起
 * Servlet implementation class PeiShui
 */
//@WebServlet("/PeiShui")
public class PeiShui extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PeiShui() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		response.getWriter().append("Served at: ").append(request.getContextPath());
		request.setCharacterEncoding("UTF-8");
		
		String data = request.getParameter("data");
		Upload upload = JSON.parseObject(data,Upload.class);
		Download download = new Download();
		
		SessionFactory sf = PersistentManager.sharemanager().getSessionFactory();
//		Session session = sf.openSession();
		Session session = sf.getCurrentSession();
		
		Transaction tx = session.beginTransaction();
		try
		{
			for(String code:upload.getStadiaCodes())
			{
				Query q = session.createQuery("from RealTimeIrrBRWaterDam _RealTimeIrrBRWaterDam where _RealTimeIrrBRWaterDam.stadiaCode=? order by _RealTimeIrrBRWaterDam.meaTime desc");
				q.setParameter(0, code);
				q.setMaxResults(1);
				
				GateWithDam dam = new GateWithDam();
				dam.setGateCode(code);
				dam.setWaterDams(q.list());
				download.result.add(dam);
			}
			
			tx.commit();
			download.setStatus(DownloadInfo.SUCCESS);
		}catch(Exception e)
		{
			download.setStatus(DownloadInfo.FAILED);
			tx.rollback();
			e.printStackTrace();
		}
		finally {
//			session.close();
		}
		
		System.out.println(JSON.toJSONString(download));
		response.setCharacterEncoding("UTF-8");
		response.getWriter().append(JSON.toJSONString(download));
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	
	
	// 上行数据
	
	public static class Upload
	{
		ArrayList<String> stadiaCodes = new ArrayList<>();

		public ArrayList<String> getStadiaCodes() {
			return stadiaCodes;
		}

		public void setStadiaCodes(ArrayList<String> stadiaCodes) {
			this.stadiaCodes = stadiaCodes;
		}
		
		
	}
	
	// 下行数据
	public static class Download
	{
//		ArrayList<GateWithDam> gateWithDams = new ArrayList<>();
//
//		public ArrayList<GateWithDam> getGateWithDams() {
//			return gateWithDams;
//		}
//
//		public void setGateWithDams(ArrayList<GateWithDam> gateWithDams) {
//			this.gateWithDams = gateWithDams;
//		}
		int status;
		
		ArrayList<GateWithDam> result = new ArrayList<>();

		
		
		public int getStatus() {
			return status;
		}

		public void setStatus(int status) {
			this.status = status;
		}

		public ArrayList<GateWithDam> getResult() {
			return result;
		}

		public void setResult(ArrayList<GateWithDam> result) {
			this.result = result;
		}

		
		
	}
	
	// 闸门和其最新信息
	public static class GateWithDam
	{
		String gateCode;// 闸门编码
		String gateName; // 闸门名称
		
		List<RealTimeIrrBRWaterDam> waterDams = new ArrayList<>();
		public String getGateCode() {
			return gateCode;
		}
		public void setGateCode(String gateCode) {
			this.gateCode = gateCode;
		}
		public String getGateName() {
			return gateName;
		}
		public void setGateName(String gateName) {
			this.gateName = gateName;
		}
		public List<RealTimeIrrBRWaterDam> getWaterDams() {
			return waterDams;
		}
		public void setWaterDams(List<RealTimeIrrBRWaterDam> waterDams) {
			this.waterDams = waterDams;
		}
		
		
		
	}

}
